//
//  KeHuViewController.m
//  LiXingZuLin
//
//  Created by ypj on 2021/10/8.
//

#import "KeHuViewController.h"
#import "KeHuListViewController.h"
#import "YJPageControlView.h"
#import "LXTopSearchView.h"
#import "LXCustomerApplyViewController.h"
@interface KeHuViewController ()<YJPageControlViewDelegate>
@property(nonatomic,strong)NSMutableArray   *   titleViewArray;
@property (nonatomic,strong)YJPageControlView *  PageControlView;
@property(nonatomic,strong) NSMutableArray    *  viewControllers;
@property(nonatomic,strong) LXTopSearchView *searchView;
@end

@implementation KeHuViewController
- (instancetype)init{
    if (self = [super init]) {
        [[kNotifyCenter rac_addObserverForName:@"customerStatusChange" object:nil] subscribeNext:^(NSNotification * _Nullable note) {
            self.selectIndex= [note.object integerValue];
            self.PageControlView.selectedIndex=self.selectIndex;
        }];
    }
    return self;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setHidden:YES];
}

- (void)viewDidLoad {
   [super viewDidLoad];
    
    self.view.backgroundColor = ColorWithF5F5F5;
    
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, StatusBarHeight+70)];
    topView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:topView];
    
    LXTopSearchView *searchView = [[LXTopSearchView alloc] initWithFrame:CGRectMake(0, StatusBarHeight-5, kScreenWidth, 70)];
    searchView.textField.placeholder = @"请输入客户姓名/手机号查询";
    searchView.textField.width = searchView.width - 120;
    searchView.textField.Y = 30;
    searchView.searchBtn.frame = CGRectMake(MaxX(searchView.textField)+15, searchView.textField.top, kScreenWidth-MaxX(searchView.textField)-30, searchView.textField.height);
    [searchView.searchBtn setBackgroundColor:mainColor];
    searchView.searchBtn.layer.cornerRadius = 3;
    searchView.searchBtn.layer.masksToBounds = YES;
    [searchView.searchBtn setTitle:@"新 增" forState:UIControlStateNormal];
    self.searchView = searchView;
    [searchView.searchBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    [topView addSubview:searchView];
    // 输入
    [searchView setChangeTextFieldBlock:^(NSString * _Nonnull text) {
        [kNotifyCenter postNotificationName:@"customerSearchText" object:text];
    }];
    [searchView setClickBtnBlock:^(NSString * _Nonnull text) {
        //新增客户
        LXCustomerApplyViewController *vc = [[LXCustomerApplyViewController alloc] init];
        vc.type = 1;
        [self pushToNextController:vc];
        
    }];
    
    self.titleViewArray = [NSMutableArray arrayWithArray:@[@"全部",@"未租车", @"已租客户",@"逾期客户"]];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = [UIColor whiteColor];
    self.viewControllers = [NSMutableArray array];
    CGRect frame = CGRectMake(0, MaxY(topView), ScreenWidth, kScreenHeight - MaxY(topView));
    for (int i = 0 ; i < self.titleViewArray.count; i++){
        KeHuListViewController *vc = [self viewControllerIndex:i];
        [self.viewControllers addObject:vc];
    }
    self.PageControlView = [[YJPageControlView alloc] initWithFrame:frame Titles:self.titleViewArray viewControllers:self.viewControllers Selectindex:self.selectIndex];
    self.PageControlView.delegate = self;
   [self.PageControlView showInViewController:self];
}
- (KeHuListViewController *)viewControllerIndex:(NSInteger)index {
    KeHuListViewController *vc = [[KeHuListViewController alloc] init];
    switch (index){
        case 0:
        {
            vc.selectIndex = 3; //全部
        }
        break;
        case 1:
        {
            vc.selectIndex = 4;// 未租客户
        }
        break;
        case 2:
        {
            vc.selectIndex = 1;// 已签约客户
        }
        break;
        case 3:
        {
            vc.selectIndex = 2;// 已逾期客户
        }
        break;
        default:
        break;
    }
    return vc;
}

- (void)SelectAtIndex:(NSInteger)index{
    self.searchView.textField.text = @"";
    self.searchView.textField.text = nil;
}

@end
